#include "stdafx.h"
#include "Headers.h"
#include "LocalDataFun.h"
#include "GameFunctions.h"

MapData binaryMap;
MapData objMap;
float deltaTime;
float prevTime;
void Gameplay(Map* This)
{
#ifdef ERROR_MESSAGES
	ZeroMemory(&_UE,sizeof(_UE));
	strcpy(_UE.functionName,"Gameplay");
	strcpy(_UE.objectType,"Map");
	strcpy(_UE.objectName,This->Name());
#endif

	const int binaryMapInit[20][15] = {
	{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
	{2,1,1,1,1,1,1,1,1,1,1,1,1,1,2},
	{2,1,2,2,2,1,2,2,2,1,2,2,2,1,2},
	{2,1,2,4,1,1,1,1,1,1,1,1,2,1,2},
	{2,1,2,1,2,1,2,1,2,1,2,5,2,1,2},
	{2,1,1,1,1,1,2,1,2,1,1,1,1,1,2},
	{2,1,2,1,2,2,2,1,2,2,2,1,2,1,2},
	{2,1,2,1,1,5,2,1,2,1,1,1,2,1,2},
	{2,1,2,1,2,2,2,1,2,2,2,1,2,1,2},
	{2,1,1,1,2,1,1,1,2,1,1,1,1,1,2},
	{2,1,2,1,1,1,2,1,1,1,2,1,2,1,2},
	{2,1,2,1,2,2,2,1,2,2,2,1,2,1,2},
	{2,1,2,1,1,3,2,1,2,1,1,1,2,1,2},
	{2,5,2,1,2,2,2,1,2,2,2,1,2,1,2},
	{2,1,1,1,1,1,2,1,2,1,1,1,1,1,2},
	{2,1,2,1,2,1,2,1,2,1,2,1,2,1,2},
	{2,1,2,1,1,1,1,1,1,1,1,1,2,1,2},
	{2,1,2,2,2,1,2,2,2,1,2,2,2,1,2},
	{2,1,1,1,1,1,1,1,1,1,1,1,1,1,2},
	{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}
	};
	
	
	binaryMap.SetBinaryMap(binaryMapInit);
	const Vector2 size = {20, 15}; objMap.Resize(size);
	static Sprite** spriteData = LoadMap(&binaryMap);
};

void PlayerFunc(Sprite* This)
{
#ifdef ERROR_MESSAGES
	ZeroMemory(&_UE,sizeof(_UE));
	strcpy(_UE.functionName,"PlayerFunc");
	strcpy(_UE.objectType,"Sprite");
	strcpy(_UE.objectName,This->Name());
#endif

	static enum states {
		idle,
		moving
	} state = idle;
	ObjData* objData = GetObjData(This);
	Vector2 dir = {objData->dirX, objData->dirY};
	Vector2 pos = {This->MapPositionX(), This->MapPositionY()};
	Vector2 nextPos = {objData->nextPosX, objData->nextPosY};
	objData->speed = 10;
	
	if (state == idle) {
	  dir = GetControllerDir();
	  if (dir != Vector2::GetZeroVector()) {
	    nextPos = GetNextTile(pos, dir);
		if (CheckCollisionTile(&binaryMap, nextPos))
	      state = moving;
	  }
	}
	else if (state == moving) {
	  if (GetCurrentTile(pos) == nextPos) {
		if (GetControllerDir() != Vector2::GetZeroVector()) {
		  dir = GetControllerDir();
		  nextPos = GetNextTile(pos, dir);
		  if (CheckCollisionTile(&binaryMap, nextPos))
	        state = moving;
		}
	    pos = GetCurrentTile(pos) * 32;
	    state = idle;
		return;
	  }
	  pos += dir * (objData->speed * deltaTime);
	  This->MapPosition(pos.x, pos.y, true);
	}
	objData->posX = pos.x;
	objData->posY = pos.y;
	objData->dirX = dir.x;
	objData->dirY = dir.y;
	objData->nextPosX = nextPos.x;
	objData->nextPosY = nextPos.y;
};

void TitleScreen(Map* This)
{
#ifdef ERROR_MESSAGES
	ZeroMemory(&_UE,sizeof(_UE));
	strcpy(_UE.functionName,"TitleScreen");
	strcpy(_UE.objectType,"Map");
	strcpy(_UE.objectName,This->Name());
#endif

	if (!pKeyboard->Nothing()) {
	  myGame->NextLevel();  
	}
};

void EnemyFunc(Sprite* This)
{
#ifdef ERROR_MESSAGES
	ZeroMemory(&_UE,sizeof(_UE));
	strcpy(_UE.functionName,"EnemyFunc");
	strcpy(_UE.objectType,"Sprite");
	strcpy(_UE.objectName,This->Name());
#endif

	static enum states
	{
	  patrol,
	  moving,
	  suspicious,
	  alert
	} state = patrol;
	ObjData* objData = GetObjData(This);
	Vector2 dir = {objData->dirX, objData->dirY};
	Vector2 pos = {This->MapPositionX(), This->MapPositionY()};
	Vector2 nextPos = {objData->nextPosX, objData->nextPosY};
	objData->speed = 10;
	
	if (objData->state == patrol)
	{
	  dir = GetRandomDir(pos, dir);
	  nextPos = GetNextTile(pos, dir);
	  if (CheckCollisionTile(&binaryMap, nextPos))
	  {
		objData->prevState = objData->state;
		objData->state = moving;
	  }
	}
	else if (objData->state == moving) {
	  if (GetCurrentTile(pos) == nextPos)
	  {
		pos = GetCurrentTile(pos) * 32;
		objData->state = patrol;
		return;
	  }
	  pos += dir * (objData->speed * deltaTime);
	  This->MapPosition(pos.x, pos.y, true);
	}
	
	objData->posX = pos.x;
	objData->posY = pos.y;
	objData->dirX = dir.x;
	objData->dirY = dir.y;
	objData->nextPosX = nextPos.x;
	objData->nextPosY = nextPos.y;
};

void UpdateObjMap(Sprite* This)
{
#ifdef ERROR_MESSAGES
	ZeroMemory(&_UE,sizeof(_UE));
	strcpy(_UE.functionName,"UpdateObjMap");
	strcpy(_UE.objectType,"Sprite");
	strcpy(_UE.objectName,This->Name());
#endif

	Vector2 pos = {This->MapPositionX(), This->MapPositionY()};
	Vector2 curTile = GetCurrentTile(pos);
	ObjData* objData = GetObjData(This);
	objMap.data[(int)curTile.x][(int)curTile.y] = objData->id;
};

void UpdateDeltaTime(Game* This)
{
#ifdef ERROR_MESSAGES
	ZeroMemory(&_UE,sizeof(_UE));
	strcpy(_UE.functionName,"UpdateDeltaTime");
	strcpy(_UE.objectType,"Game");
	strcpy(_UE.objectName,This->Name());
#endif

	float curTime = time(NULL);
	deltaTime = (curTime - prevTime) / 1000;
	prevTime = curTime;
	
	
	
	deltaTime = 0.1666;
};

void AIController(Sprite* This)
{
#ifdef ERROR_MESSAGES
	ZeroMemory(&_UE,sizeof(_UE));
	strcpy(_UE.functionName,"AIController");
	strcpy(_UE.objectType,"Sprite");
	strcpy(_UE.objectName,This->Name());
#endif

	
};

void MouseController(FunCursor* This)
{
#ifdef ERROR_MESSAGES
	ZeroMemory(&_UE,sizeof(_UE));
	strcpy(_UE.functionName,"MouseController");
	strcpy(_UE.objectType,"FunCursor");
#endif

	static Sprite* selectedSprite;
	
	if (pMouse->IsPressed(DIM_LEFT))
	{
	  Sprite* clickSprite = GetClickedSprite(pMouse->X(), pMouse->Y());
	  if (clickSprite)
		selectedSprite = clickSprite;
	  else if (selectedSprite)
	  {
		//move sprite
	  }
	}
};

